﻿using UnityEngine;

namespace Yoozoo.Art
{
    public class ShadowSphereFollow : MonoBehaviour
    {
        public Camera FollowCamera;

        public Transform ShadowSphere;

        private Vector3 cameraPosition;

        // Update is called once per frame
        void LateUpdate()
        {
            if (FollowCamera && ShadowSphere)
            {
                if (cameraPosition != FollowCamera.transform.position)
                {
                    cameraPosition = FollowCamera.transform.position;
                    var ray = FollowCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f));
                    var origin = ray.origin;
                    var direction = ray.direction;
                    var p = origin.y / direction.y;
                    var hitPoint = origin - direction * p;
                    ShadowSphere.position = hitPoint;
                }
            }
        }
    }
}
